<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    width="35%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      :disabled="mode == 'show'"
      label-width="auto"
      label-position="right"
      class="noFlex"
    >
      <el-form-item label="app版本" prop="versionNumber">
        <el-input
          v-model="form.versionNumber"
          clearable
          placeholder="请输入版本号"
        ></el-input>
      </el-form-item>
      <el-form-item label="下载地址" prop="downloadLink">
        <el-input
          v-model="form.downloadLink"
          clearable
          placeholder="请输入下载地址（http://download.guanyu3d.cn/包名称.apk）"
        ></el-input>
      </el-form-item>
      <el-form-item label="创建人">
        <el-input
          v-model="form.createAdmin"
          clearable
          placeholder="请输入创建人名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="form.remark"
          clearable
          placeholder="请输入备注"
          type="textarea"
          rows="5"
        ></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ['success', 'closed'],
  data () {
    return {
      mode: "add",
      titleMap: {
        add: '新增',
        edit: '编辑',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      //表单数据
      form: {
        createAdmin: '',//创建人
        downloadLink: 'http://download.guanyu3d.cn/包名称.apk',//	下载地址
        remark: '',
        // isNowVersion:0,//是否当前版本  0否1是
        versionNumber: ''//版本号
      },
      //验证规则
      rules: {
        versionNumber: [
          { required: true, message: '请输入版本号', }
        ],
        downloadLink: [
          { required: true, message: '请输入下载地址', }
        ],
      }
    }
  },
  mounted () {

  },

  methods: {
    //显示
    open (mode = 'add') {
      this.mode = mode
      this.visible = true
      let userInfo = this.$TOOL.data.get('USER_INFO')
      this.form.createAdmin = userInfo.userName
      return this
    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let res = null
          if (this.mode == 'add') {
            res = await this.$API.version.addAppVersion.post(this.form)
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit('success')
            this.visible = false
            this.$message.success("操作成功")
          } else {
            this.$alert(res.message, "提示", { type: 'error' })
          }
        }
      })
    },

    //表单注入数据
    setData (data) {
      this.form = data
    },

  }
}
</script>

<style lang="scss" scoped>
</style>
